home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Nuntius 1.2 / src / MyToolSource / MakeArticleIndex.cp < prev    next >
Encoding:
Text File  |  1992-04-27  |  4.3 KB  |  147 lines  |  [TEXT/MPS ]

  1. #include <CType.h>
  2. #include <Types.h>
  3. #include <stdio.h>
  4. #include <stream.h>
  5. #include <CursorCtl.h>
  6. #include <StdLib.h>
  7. #include <FCntl.h>
  8.  
  9. long headPos = 0, bodyPos = 0;
  10. long curPos = 0;
  11. char s[1000], ucs[1000];
  12. char* s2;
  13. Boolean inHead = false, inBody = false;
  14.  
  15. void FlushBody(ostream& outfile)
  16. {
  17.     if (inBody) {
  18.         outfile << " " << dec(curPos - bodyPos, 10);
  19.         outfile << " " << dec(headPos, 10);
  20.         outfile << " " << dec(curPos - headPos, 10) << "\n";
  21.         inBody = false;
  22.     }
  23. }
  24.  
  25. int CheckOpen(int fd)
  26. {
  27.     if (fd == 0) {
  28.         cerr << "\nError opening or creating a file\n";
  29.         exit(1);
  30.     }
  31.     return fd;
  32. }
  33.  
  34. int main()
  35. {
  36.     InitCursorCtl(nil);
  37.     
  38.     int fdArticles    = CheckOpen(open ("articles", 0));
  39.     int fdIndex            = CheckOpen(creat("article_index"));
  40.     int fdFrom            = CheckOpen(creat("qFrom"));
  41.     int fdSubject        = CheckOpen(creat("qSubject"));
  42.     int fdMsgID            = CheckOpen(creat("qMessage-ID"));
  43.     int fdRefs            = CheckOpen(creat("qReferences"));
  44.     int fdRange            = CheckOpen(creat("qRange"));
  45.     
  46.     char articlesBuf[20000], indexBuf[2000];
  47.     char fromBuf[2000], subjectBuf[2000], msgIDBuf[2000], refsBuf[2000];
  48.     char rangeBuf[256];
  49.  
  50.     filebuf fbArticles    (fdArticles,    articlesBuf,    sizeof(articlesBuf));
  51.     filebuf fbIndex            (fdIndex,            indexBuf,            sizeof(indexBuf));
  52.     filebuf fbFrom            (fdFrom,            fromBuf,            sizeof(fromBuf));
  53.     filebuf fbSubject        (fdSubject,        subjectBuf,        sizeof(subjectBuf));
  54.     filebuf fbMsgID            (fdMsgID,            msgIDBuf,            sizeof(msgIDBuf));
  55.     filebuf fbRefs            (fdRefs,            refsBuf,            sizeof(refsBuf));
  56.     filebuf fbRange            (fdRange,            rangeBuf,            sizeof(rangeBuf));
  57.  
  58.     istream fileArticles(&fbArticles);
  59.     ostream fileIndex        (&fbIndex);
  60.     ostream fileFrom        (&fbFrom);
  61.     ostream fileSubject    (&fbSubject);
  62.     ostream fileMsgID        (&fbMsgID);
  63.     ostream fileRefs        (&fbRefs);
  64.     ostream fileRange        (&fbRange);
  65.  
  66.     Boolean gotFirstId = false;
  67.     Boolean gotFrom = false, gotSubject = false, gotMsgID = false, gotRefs = false;
  68.     char IDstr[10];
  69.     char firstIDStr[10];
  70.     long lastID = -1;
  71.     int i;
  72.     while (!fileArticles.eof()) {
  73.         SpinCursor(1);
  74.         fileArticles.getline(s, 900);
  75.         if (s[0] == '@' && strncmp(s, "@@@@ /usr/spool", 15) == 0) {
  76.             gotFrom = false, gotSubject = false, gotMsgID = false, gotRefs = false;
  77.             for (i = strlen(s); s[i - 1] != ' '; i--);
  78.             strcpy(IDstr, s + i);
  79.             if (!gotFirstId) {
  80.                 fileRange << IDstr << " ";
  81.                 strcpy(firstIDStr, IDstr);
  82.                 gotFirstId = true;
  83.             }
  84.             FlushBody(fileIndex);
  85.             long ID;
  86.             sscanf(IDstr, "%ld", &ID);
  87.             if (lastID != -1) {
  88.                 while (++lastID < ID) {
  89.                     char lastIDstr[200];
  90.                     sprintf(lastIDstr, "%ld", lastID);
  91.                     fileIndex << dec(lastID, 10) << "          0          0          0          0          0          0\n";
  92.                     fileFrom << lastIDstr << " (none)\n";
  93.                     fileSubject << lastIDstr << " (none)\n";
  94.                     fileMsgID << lastIDstr << " (none)\n";
  95.                     fileRefs << lastIDstr << " (none)\n";
  96.                 }
  97.             }
  98.             lastID = ID;
  99.             inHead = true;
  100.             s2 = s+(strlen(s)-1);
  101.             while (*(s2-1) != ' ') s2--;
  102.             headPos = curPos + strlen(s) + 1; // after @@@@ line, start with Path:
  103.             for (i = strlen(s2) + 1; i <= 10; i++) fileIndex << " ";
  104.             fileIndex << s2 << " " << dec(headPos, 10);
  105.         }
  106.         else if (inHead) {
  107.             strcpy(ucs, s);
  108.             for (i = 0; ucs[i] != 0; i++) ucs[i] = toupper(ucs[i]);
  109.             if (strlen(s) == 0) {
  110.                 fileIndex << " " << dec(curPos - headPos, 10) << " " << dec(curPos + 1, 10);
  111.                 inHead = false;
  112.                 inBody = true;
  113.                 bodyPos = curPos;
  114.                 if (!gotFrom)    fileFrom            << IDstr << " (none)\n";
  115.                 if (!gotSubject) fileSubject    << IDstr << " (none)\n";
  116.                 if (!gotMsgID)   fileMsgID        << IDstr << " (none)\n";
  117.                 if (!gotRefs)    fileRefs            << IDstr << " (none)\n";
  118.             } else if (strncmp(ucs, "FROM: ", 6) == 0) {
  119.                 fileFrom << IDstr << " " << s + 6 << "\n";
  120.                 gotFrom = true;
  121.             } else if (strncmp(ucs, "SUBJECT: ", 9) == 0) {
  122.                 fileSubject << IDstr << " " << s + 9 << "\n";
  123.                 gotSubject = true;
  124.             } else if (strncmp(ucs, "MESSAGE-ID: ", 12) == 0) {
  125.                 fileMsgID << IDstr << " " << s + 12 << "\n";
  126.                 gotMsgID = true;
  127.             } else if (strncmp(ucs, "REFERENCES: ", 12) == 0) {
  128.                 fileRefs << IDstr << " " << s + 12 << "\n";
  129.                 gotRefs = true;
  130.             }
  131.         }
  132.         else if (inBody) {
  133.         }
  134.         curPos += strlen(s) + 1;
  135.     }
  136.     curPos--; // dette er nødvendigt for at kunne læse sidste artikkel, desværre!
  137.     FlushBody(fileIndex);
  138.     if (gotFirstId)
  139.     {
  140.         fileRange << IDstr << " ";
  141.         fileRange << firstIDStr << " " << IDstr << "\n";
  142.     }
  143.     else
  144.         fileRange << "1 0 1 0\n";
  145.     return 0;
  146. }
  147.